package com.kugou.test.helper;

import java.io.File;
import java.io.FileInputStream;
import java.math.BigInteger;
import java.security.MessageDigest;

import android.os.Environment;

public class FileHelper {
	static public String joinPath(String path1, String path2) {
		return new File(path1, path2).toString();
	}

	static public void del(String file){
		File f = new File(file);
		if(f.exists()) f.delete();
	}

	static public String getSdCard() {
		return Environment.getExternalStorageDirectory().getPath();
	}
	
	static public String getMd5(String fileName){
		File file = new File(fileName);
		if (!file.isFile()) {
			   return null;
			  }
			  MessageDigest digest = null;
			  FileInputStream in = null;
			  byte buffer[] = new byte[1024];
			  int len;
			  try {
				  digest = MessageDigest.getInstance("MD5");
				  in = new FileInputStream(file);
				  while ((len = in.read(buffer, 0, 1024)) != -1) {
					  digest.update(buffer, 0, len);
				  }
				  in.close();
			  } catch (Exception e) {
				  e.printStackTrace();
				  return null;
			  }
			  BigInteger bigInt = new BigInteger(1, digest.digest());
			  return bigInt.toString(16);
	}
	
	static public String getContent(String fileName){
		File file=new File(fileName);
        Long filelength = file.length();     //获取文件长度
        byte[] filecontent = new byte[filelength.intValue()];
        try {
            FileInputStream in = new FileInputStream(file);
            in.read(filecontent);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new String(filecontent);//返回文件内容,默认编码
	}
	
	static public boolean isSame(String file1, String file2){
		String file1Md5 =  FileHelper.getMd5(file1);
		String file2Md5 = FileHelper.getMd5(file2);
		return file1Md5.equals(file2Md5);
	}
}
